package com.jqsoft.launcher.patientdb;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jqsoft.launcher.activity.bean.HBPGuanLi_PersonInfo;
import com.jqsoft.launcher.activity.bean.LittleChildBean;
import com.jqsoft.launcher.activity.bean.MajorPsychosis_PersonBean;
import com.jqsoft.launcher.activity.bean.PregnantWomenBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 患者数据库更新类
 * 
 * @author gujunqi date：2014-3-31
 */
public class PatientDao {

	private Context context;
	private PatientDbOpenHelper dbhelper;

	public PatientDao(Context context) {
		this.context = context;
		DatabaseContext dbContext = new DatabaseContext(context);
		dbhelper = new PatientDbOpenHelper(dbContext);
//		dbhelper = new PatientDbOpenHelper(context);
	}

	// // 查询是否有这个功能
	// public boolean find(String functionName) {
	// boolean result = false;
	// SQLiteDatabase db = dbhelper.getReadableDatabase();
	// if (db.isOpen()) {
	// Cursor curosr = db.query("function", null, "functionName=?",
	// new String[] { functionName }, null, null, null);
	// if (curosr.moveToFirst()) {
	// result = true;
	// }
	// curosr.close();
	// db.close();
	// }
	// return result;
	// }
	/**
	 * 获取高血压查到的信息
	 * 
	 * @return
	 */
	public List<HBPGuanLi_PersonInfo> getGroupList(String chaKey,
                                                   String chaValue) {
		SQLiteDatabase db = dbhelper.getReadableDatabase();
		List<HBPGuanLi_PersonInfo> groupLists = new ArrayList<HBPGuanLi_PersonInfo>();
		HBPGuanLi_PersonInfo info;
		if (db.isOpen()) {
			Cursor cursor = db.rawQuery("select * from highblood where "
					+ chaKey + " like ?",
					new String[] { "%" + chaValue.replace(" ", "%") + "%" });
			while (cursor.moveToNext()) {
				String sPersonID = cursor.getString(cursor
						.getColumnIndex("sPersonID"));
				String sNo = cursor.getString(cursor.getColumnIndex("sNo"));
				String sPersonName = cursor.getString(cursor
						.getColumnIndex("sPersonName"));
				String sSexName = cursor.getString(cursor
						.getColumnIndex("sSexName"));
				String dBirthday = cursor.getString(cursor
						.getColumnIndex("dBirthday"));
				String sCardNo = cursor.getString(cursor
						.getColumnIndex("sCardNo"));
				String sGradeName = cursor.getString(cursor
						.getColumnIndex("sGradeName"));
				String sDiagnosedHospital = cursor.getString(cursor
						.getColumnIndex("sDiagnosedHospital"));
				String dDiagnosedTime = cursor.getString(cursor
						.getColumnIndex("dDiagnosedTime"));
				String sInputDeptName = cursor.getString(cursor
						.getColumnIndex("sInputDeptName"));
				String visitingCount = cursor.getString(cursor
						.getColumnIndex("visitingCount"));
				String healthyCheckUpCount = cursor.getString(cursor
						.getColumnIndex("healthyCheckUpCount"));
				String iState = cursor.getString(cursor
						.getColumnIndex("iState"));
				String sAreaCode = cursor.getString(cursor
						.getColumnIndex("sAreaCode"));
				String sphoto = cursor.getString(cursor.getColumnIndex("sphoto"));
				String sphoto1 = cursor.getString(cursor.getColumnIndex("sphoto1"));
				info = new HBPGuanLi_PersonInfo(sPersonID, sNo, sPersonName,
						sSexName, dBirthday, sCardNo, sGradeName,
						sDiagnosedHospital, dDiagnosedTime, sInputDeptName,
						visitingCount, healthyCheckUpCount, iState, sAreaCode,sphoto);
				groupLists.add(info);
				info = null;
			}
			cursor.close();
			db.close();
		}
		return groupLists;
	}

	// // 0代表false 1代表true
	// // insert into person (name,phone) values ('zhangsan','120')
	public boolean add(String sPersonID, String sNo, String sPersonName,
			String sSexName, String dBirthday, String sCardNo,
			String sGradeName, String sDiagnosedHospital,
			String dDiagnosedTime, String sInputDeptName, String VisitingCount,
			String HealthyCheckUpCount, String iState,String sphoto,String sphoto1) {
		long result = 0;
		// if (find(functionName + "")) {
		// return false;
		// }
		SQLiteDatabase db = dbhelper.getWritableDatabase();
		if (db.isOpen()) {
			// db.execSQL("insert into person (name,phone) values ('zhangsan','120')")
			// db.execSQL("insert into person (name,phone) values (?,?)", new
			// Object[]{name,phone});
			ContentValues values = new ContentValues();
			values.put("sPersonID", sPersonID);
			values.put("sNo", sNo);
			values.put("sPersonName", sPersonName);
			values.put("sSexName", sSexName);
			values.put("dBirthday", dBirthday);
			values.put("sCardNo", sCardNo);
			values.put("sGradeName", sGradeName);
			values.put("sDiagnosedHospital", sDiagnosedHospital);
			values.put("dDiagnosedTime", dDiagnosedTime);
			values.put("sInputDeptName", sInputDeptName);
			values.put("VisitingCount", VisitingCount);
			values.put("HealthyCheckUpCount", HealthyCheckUpCount);
			values.put("iState", iState);
			values.put("sphoto",sphoto);
			values.put(("sphoto1"),sphoto1);
			result = db.insert("highblood", null, values);
			db.close();
		}
		if (result == -1) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 增加到糖尿病中
	 * 
	 * @param sPersonID
	 * @param sNo
	 * @param sPersonName
	 * @param sSexName
	 * @param dBirthday
	 * @param sCardNo
	 * @param sGradeName
	 * @param sDiagnosedHospital
	 * @param dDiagnosedTime
	 * @param sInputDeptName
	 * @param VisitingCount
	 * @param HealthyCheckUpCount
	 * @param iState
	 * @return
	 */
	public boolean addTNB(String sPersonID, String sNo, String sPersonName,
			String sSexName, String dBirthday, String sCardNo,
			String sGradeName, String sDiagnosedHospital,
			String dDiagnosedTime, String sInputDeptName, String VisitingCount,
			String HealthyCheckUpCount, String iState,String sphoto,String sphoto1) {
		long result = 0;
		// if (find(functionName + "")) {
		// return false;
		// }
		SQLiteDatabase db = dbhelper.getWritableDatabase();
		if (db.isOpen()) {
			// db.execSQL("insert into person (name,phone) values ('zhangsan','120')")
			// db.execSQL("insert into person (name,phone) values (?,?)", new
			// Object[]{name,phone});
			ContentValues values = new ContentValues();
			values.put("sPersonID", sPersonID);
			values.put("sNo", sNo);
			values.put("sPersonName", sPersonName);
			values.put("sSexName", sSexName);
			values.put("dBirthday", dBirthday);
			values.put("sCardNo", sCardNo);
			values.put("sGradeName", sGradeName);
			values.put("sDiagnosedHospital", sDiagnosedHospital);
			values.put("dDiagnosedTime", dDiagnosedTime);
			values.put("sInputDeptName", sInputDeptName);
			values.put("VisitingCount", VisitingCount);
			values.put("HealthyCheckUpCount", HealthyCheckUpCount);
			values.put("iState", iState);
			values.put("sphoto",sphoto);
			values.put("sphoto",sphoto1);
			result = db.insert("diabetesmellitus", null, values);
			db.close();
		}
		if (result == -1) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 获取糖尿病查到的信息
	 * 
	 * @return
	 */
	public List<HBPGuanLi_PersonInfo> getTNBGroupList(String chaKey,
                                                      String chaValue) {
		SQLiteDatabase db = dbhelper.getReadableDatabase();
		List<HBPGuanLi_PersonInfo> groupLists = new ArrayList<HBPGuanLi_PersonInfo>();
		HBPGuanLi_PersonInfo info;
		if (db.isOpen()) {
			Cursor cursor = db.rawQuery("select * from diabetesmellitus where "
					+ chaKey + " like ?",
					new String[] { "%" + chaValue.replace(" ", "%") + "%" });
			// Cursor cursor =
			// db.rawQuery("select * from diabetesmellitus where "+chaKey+"=?",
			// new String[]{chaValue});
			while (cursor.moveToNext()) {
				String sPersonID = cursor.getString(cursor
						.getColumnIndex("sPersonID"));
				String sNo = cursor.getString(cursor.getColumnIndex("sNo"));
				String sPersonName = cursor.getString(cursor
						.getColumnIndex("sPersonName"));
				String sSexName = cursor.getString(cursor
						.getColumnIndex("sSexName"));
				String dBirthday = cursor.getString(cursor
						.getColumnIndex("dBirthday"));
				String sCardNo = cursor.getString(cursor
						.getColumnIndex("sCardNo"));
				String sGradeName = cursor.getString(cursor
						.getColumnIndex("sGradeName"));
				String sDiagnosedHospital = cursor.getString(cursor
						.getColumnIndex("sDiagnosedHospital"));
				String dDiagnosedTime = cursor.getString(cursor
						.getColumnIndex("dDiagnosedTime"));
				String sInputDeptName = cursor.getString(cursor
						.getColumnIndex("sInputDeptName"));
				String visitingCount = cursor.getString(cursor
						.getColumnIndex("visitingCount"));
				String healthyCheckUpCount = cursor.getString(cursor
						.getColumnIndex("healthyCheckUpCount"));
				String iState = cursor.getString(cursor
						.getColumnIndex("iState"));
				String sAreaCode = cursor.getString(cursor
						.getColumnIndex("sAreaCode"));
				String sphoto = cursor.getString(cursor.getColumnIndex("sphoto"));
				String sphoto1 = cursor.getString(cursor.getColumnIndex("sphoto1"));
				info = new HBPGuanLi_PersonInfo(sPersonID, sNo, sPersonName,
						sSexName, dBirthday, sCardNo, sGradeName,
						sDiagnosedHospital, dDiagnosedTime, sInputDeptName,
						visitingCount, healthyCheckUpCount, iState, sAreaCode,sphoto);
				groupLists.add(info);
				info = null;
			}
			cursor.close();
			db.close();
		}
		return groupLists;
	}

	public boolean deleteAll() {
		boolean result = false;
		SQLiteDatabase db = dbhelper.getWritableDatabase();
		if (db.isOpen()) {
			long raw = db.delete("highblood", null, null);
			raw = db.delete("diabetesmellitus", null, null);
			raw = db.delete("majorpsychosis", null, null);
			raw = db.delete("childcare", null, null);
			raw = db.delete("pregnantwomen", null, null);
			if (raw > 0) {
				result = true;
			}
			db.close();
		}
		return result;
	}

	public boolean addMajor(String sPersonID, String sNo, String sPersonName,
			String sSexName, String dBirthday, String sCardNo,
			String sGuardianName, String sNowDiagnosis, String TreatmentEffect,
			String dFillTime, String sDoctorName, String sInputDeptName,
			String VisitingCount, String ExaminationCount, String iState,String sphoto) {
		long result = 0;
		// if (find(functionName + "")) {
		// return false;
		// }
		SQLiteDatabase db = dbhelper.getWritableDatabase();
		if (db.isOpen()) {
			// db.execSQL("insert into person (name,phone) values ('zhangsan','120')")
			// db.execSQL("insert into person (name,phone) values (?,?)", new
			// Object[]{name,phone});
			ContentValues values = new ContentValues();
			values.put("sPersonID", sPersonID);
			values.put("sNo", sNo);
			values.put("sPersonName", sPersonName);
			values.put("sSexName", sSexName);
			values.put("dBirthday", dBirthday);
			values.put("sCardNo", sCardNo);
			values.put("sGuardianName", sGuardianName);
			values.put("sNowDiagnosis", sNowDiagnosis);
			values.put("TreatmentEffect", TreatmentEffect);
			values.put("dFillTime", dFillTime);
			values.put("sDoctorName", sDoctorName);
			values.put("sInputDeptName", sInputDeptName);
			values.put("VisitingCount", VisitingCount);
			values.put("ExaminationCount", ExaminationCount);
			values.put("iState", iState);
			values.put("sphoto",sphoto);
			result = db.insert("majorpsychosis", null, values);
			db.close();
		}
		if (result == -1) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 获取重型精神病查到的信息
	 * 
	 * @return
	 */
	public List<MajorPsychosis_PersonBean> getMajorGroupList(String chaKey,
                                                             String chaValue) {
		SQLiteDatabase db = dbhelper.getReadableDatabase();
		List<MajorPsychosis_PersonBean> groupLists = new ArrayList<MajorPsychosis_PersonBean>();
		MajorPsychosis_PersonBean info;
		if (db.isOpen()) {
			// Cursor cursor = db.rawQuery(
			// "select * from majorpsychosis where sPersonName=?",
			// new String[] { "陈艳林" });
			Cursor cursor = db.rawQuery("select * from majorpsychosis where "
					+ chaKey + " like ?",
					new String[] { "%" + chaValue.replace(" ", "%") + "%" });
			// Cursor cursor =
			// db.rawQuery("select * from majorpsychosis where "+chaKey+" = ?",
			// new String[]{chaValue});
			while (cursor.moveToNext()) {
				String sPersonID = cursor.getString(cursor
						.getColumnIndex("sPersonID"));
				String sNo = cursor.getString(cursor.getColumnIndex("sNo"));
				String sPersonName = cursor.getString(cursor
						.getColumnIndex("sPersonName"));
				String sSexName = cursor.getString(cursor
						.getColumnIndex("sSexName"));
				String dBirthday = cursor.getString(cursor
						.getColumnIndex("dBirthday"));
				String sCardNo = cursor.getString(cursor
						.getColumnIndex("sCardNo"));
				String sGuardianName = cursor.getString(cursor
						.getColumnIndex("sGuardianName"));

				String sNowDiagnosis = cursor.getString(cursor
						.getColumnIndex("sNowDiagnosis"));
				String treatmentEffect = cursor.getString(cursor
						.getColumnIndex("treatmentEffect"));
				String dFillTime = cursor.getString(cursor
						.getColumnIndex("dFillTime"));
				String sDoctorName = cursor.getString(cursor
						.getColumnIndex("sDoctorName"));
				String sInputDeptName = cursor.getString(cursor
						.getColumnIndex("sInputDeptName"));
				String visitingCount = cursor.getString(cursor
						.getColumnIndex("visitingCount"));
				String examinationCount = cursor.getString(cursor
						.getColumnIndex("examinationCount"));
				String iState = cursor.getString(cursor
						.getColumnIndex("iState"));
				String sAreaCode = cursor.getString(cursor
						.getColumnIndex("sAreaCode"));
				String sphoto = cursor.getString(cursor.getColumnIndex("sphoto"));
				info = new MajorPsychosis_PersonBean(sPersonID, sNo,
						sPersonName, sSexName, dBirthday, sCardNo,
						sGuardianName, sNowDiagnosis, treatmentEffect,
						dFillTime, sDoctorName, sInputDeptName, visitingCount,
						examinationCount, iState, sAreaCode,  sphoto);
				groupLists.add(info);
				info = null;
			}
			cursor.close();
			db.close();
		}
		return groupLists;
	}

	public boolean addChild(String sPersonID, String sNo, String sPersonName,
			String sSexName, String dBirthday, String sCardNo, String sSexCode,
			String gKey, String sPhone, String sRegisterNo, String dRegisterDate,String sphoto) {
		long result = 0;
		SQLiteDatabase db = dbhelper.getWritableDatabase();
		if (db.isOpen()) {
			ContentValues values = new ContentValues();
			values.put("sPersonID", sPersonID);
			values.put("sNo", sNo);
			values.put("sPersonName", sPersonName);
			values.put("sSexName", sSexName);
			values.put("dBirthday", dBirthday);
			values.put("sCardNo", sCardNo);
			values.put("sSexCode", sSexCode);
			values.put("gKey", gKey);
			values.put("sPhone", sPhone);
			values.put("sRegisterNo", sRegisterNo);
			values.put("dRegisterDate", dRegisterDate);
			values.put("sphoto",sphoto);
			result = db.insert("childcare", null, values);
			db.close();
		}
		if (result == -1) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 获取儿童查到的信息
	 * 
	 * @return
	 */
	public List<LittleChildBean> getlittleChildList(String chaKey,
                                                    String chaValue) {
		SQLiteDatabase db = dbhelper.getReadableDatabase();
		List<LittleChildBean> groupLists = new ArrayList<LittleChildBean>();
		LittleChildBean info;
		if (db.isOpen()) {
			Cursor cursor = db.rawQuery("select * from childcare where "
					+ chaKey + " like ?",
					new String[] { "%" + chaValue.replace(" ", "%") + "%" });
			while (cursor.moveToNext()) {
				// String sPersonID,String sNo, String sPersonName, String
				// sSexName,
				// String dBirthday,String sCardNo, String sSexCode,String gKey,
				// String sPhone,String sRegisterNo,String dRegisterDate
				String sPersonID = cursor.getString(cursor
						.getColumnIndex("sPersonID"));
				String sNo = cursor.getString(cursor.getColumnIndex("sNo"));
				String sPersonName = cursor.getString(cursor
						.getColumnIndex("sPersonName"));
				String sSexName = cursor.getString(cursor
						.getColumnIndex("sSexName"));
				String dBirthday = cursor.getString(cursor
						.getColumnIndex("dBirthday"));
				String sCardNo = cursor.getString(cursor
						.getColumnIndex("sCardNo"));
				String sSexCode = cursor.getString(cursor
						.getColumnIndex("sSexCode"));

				String gKey = cursor.getString(cursor.getColumnIndex("gKey"));
				String VisitingCount = cursor.getString(cursor
						.getColumnIndex("VisitingCount"));
				String sPhone = cursor.getString(cursor
						.getColumnIndex("sPhone"));
				String sRegisterNo = cursor.getString(cursor
						.getColumnIndex("sRegisterNo"));
				String dRegisterDate = cursor.getString(cursor
						.getColumnIndex("dRegisterDate"));
				String CIRsAreaCode = cursor.getString(cursor
						.getColumnIndex("CIRsAreaCode"));
				String sphoto = cursor.getString(cursor.getColumnIndex("sphoto"));
				info = new LittleChildBean(sPersonID, sNo, sPersonName,
						sSexName, sSexCode, dBirthday, sCardNo, sPhone,
						sRegisterNo, dRegisterDate, gKey, VisitingCount,
						CIRsAreaCode,sphoto);
				groupLists.add(info);
				info = null;
			}
			cursor.close();
			db.close();
		}
		return groupLists;
	}

	// private String sPersonID; //个人唯一标识
	// private String sNo;//档案编号
	// private String sPersonName;//姓名
	// private String sSexCode;//性别2代表女
	// private String dBirthday;//出生日期（年龄需要计算）
	// private String sCardNo;//证件号码
	// private String sPhone;//联系方式
	// private String iPregnancytime;//孕次
	// private String gKey;
	// private String iState;//是否管理（-1否 1是）

	public boolean addpregnantWomen(String sPersonID, String sNo,
			String sPersonName, String sSexCode, String dBirthday,
			String sCardNo, String sPhone, String iPregnancytime, String gKey,
			String iState,String sphoto) {
		long result = 0;
		SQLiteDatabase db = dbhelper.getWritableDatabase();
		if (db.isOpen()) {
			ContentValues values = new ContentValues();
			values.put("sPersonID", sPersonID);
			values.put("sNo", sNo);
			values.put("sPersonName", sPersonName);
			values.put("sSexCode", sSexCode);
			values.put("dBirthday", dBirthday);
			values.put("sCardNo", sCardNo);
			values.put("sPhone", sPhone);
			values.put("iPregnancytime", iPregnancytime);
			values.put("gKey", gKey);
			values.put("iState", iState);
			values.put("sphoto",sphoto);
			result = db.insert("pregnantwomen", null, values);
			db.close();
		}
		if (result == -1) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 获取孕产妇查到的信息
	 * 
	 * @return
	 */
	public List<PregnantWomenBean> getpregnantWomenList(String chaKey,
                                                        String chaValue) {
		SQLiteDatabase db = dbhelper.getReadableDatabase();
		List<PregnantWomenBean> groupLists = new ArrayList<PregnantWomenBean>();
		PregnantWomenBean info;
		if (db.isOpen()) {
			Cursor cursor = db.rawQuery("select * from pregnantwomen where "
					+ chaKey + " like ?",
					new String[] { "%" + chaValue.replace(" ", "%") + "%" });
			while (cursor.moveToNext()) {
				String sPersonID = cursor.getString(cursor
						.getColumnIndex("sPersonID"));
				String sNo = cursor.getString(cursor.getColumnIndex("sNo"));
				String sPersonName = cursor.getString(cursor
						.getColumnIndex("sPersonName"));
				String sSexCode = cursor.getString(cursor
						.getColumnIndex("sSexCode"));
				String dBirthday = cursor.getString(cursor
						.getColumnIndex("dBirthday"));
				String sCardNo = cursor.getString(cursor
						.getColumnIndex("sCardNo"));
				String sPhone = cursor.getString(cursor
						.getColumnIndex("sPhone"));

				String iPregnancytime = cursor.getString(cursor
						.getColumnIndex("iPregnancytime"));
				String iPostpartumVisit = cursor.getString(cursor
						.getColumnIndex("iPostpartumVisit"));
				String gKey = cursor.getString(cursor.getColumnIndex("gKey"));
				String iState = cursor.getString(cursor
						.getColumnIndex("iState"));
				String sAreaCode = cursor.getString(cursor
						.getColumnIndex("sAreaCode"));
				String sphoto = cursor.getString(cursor.getColumnIndex("sphoto"));
				info = new PregnantWomenBean(sPersonID, sNo, sPersonName,
						sSexCode, dBirthday, sCardNo, sPhone, iPregnancytime,
						iState, gKey, iPostpartumVisit, sAreaCode,sphoto);
				groupLists.add(info);
				info = null;
			}
			cursor.close();
			db.close();
		}
		return groupLists;
	}
}
